package com.leilei.dao.impl;

import com.leilei.dao.ICommodityVersion;
import com.leilei.entity.CommodityVersion;
import com.leilei.util.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class CommodityVersionImpl implements ICommodityVersion {

    private QueryRunner queryRunner = new QueryRunner(DBUtil.getDataSource());

    @Override
    public List<CommodityVersion> selectCommodityVersionByPid(int pid) {

        try {
            return queryRunner.query("select pvid, pid, versionname, oprice, cprice, num, vdefault from commodityversion where pid = ?",
                    new BeanListHandler<CommodityVersion>(CommodityVersion.class), pid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return null;
    }

    @Override
    public CommodityVersion getCommodityVersionByPvid(int pvid) {

        try {
            return queryRunner.query("select pvid, pid, versionname, oprice, cprice, num, vdefault from commodityversion where pvid = ?",
                    new BeanHandler<CommodityVersion>(CommodityVersion.class), pvid);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return null;
    }

    @Override
    public int insertCommodityVersion(CommodityVersion commodityVersion) {

        String sql = "insert into commodityversion(pid, versionname, oprice, cprice, num) values(?, ?, ?, ?, ?)";

        try {

            return queryRunner.update(sql,
                    commodityVersion.getPid(),
                    commodityVersion.getVersionname(),
                    commodityVersion.getOprice(),
                    commodityVersion.getCprice(),
                    commodityVersion.getNum());

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteCommodityVersionByPvid(int pvid) {

        String sql = "delete from commodityversion where pvid = ?";

        try {

            return queryRunner.update(sql, pvid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int updateCommodityVersion(CommodityVersion commodityVersion) {

        String sql = "update commodityversion set versionname = ?, oprice = ?, cprice = ?, num = ? where pvid = ?";

        try {

            return queryRunner.update(sql,
                    commodityVersion.getVersionname(),
                    commodityVersion.getOprice(),
                    commodityVersion.getCprice(),
                    commodityVersion.getNum(),
                    commodityVersion.getPvid());

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

}
